package com.githan.bootsecurity.config;

import com.alibaba.fastjson.JSON;
import com.githan.bootsecurity.response.Result;
import com.githan.bootsecurity.response.ResultCode;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author https://github.com/Han56
 * @date 2021/4/11 15:17
 */
@Component
public class MyAuthenticationFailureHandler extends JSONAuthentication implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result result = null;
        if (e instanceof AccountExpiredException){
            //账号过期
            result=Result.error(ResultCode.USER_ACCOUNT_EXPIRED);
        }else if (e instanceof BadCredentialsException){
            //密码错误
            result=Result.error(ResultCode.USER_CREDENTIALS_ERROR);
        }else if (e instanceof CredentialsExpiredException){
            //密码过期
            result=Result.error(ResultCode.USER_CREDENTIALS_EXPIRED);
        }else if (e instanceof DisabledException){
            //账号不可用
            result=Result.error(ResultCode.USER_ACCOUNT_DISABLE);
        }else if (e instanceof LockedException){
            //账号锁定
            result=Result.error(ResultCode.USER_ACCOUNT_LOCKED);
        }else if (e instanceof InternalAuthenticationServiceException){
            //用户不存在
            result=Result.error(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }else {
            //其他未知错误
            result=Result.error(ResultCode.COMMON_FAIL);
        }
        this.WriteJSON(httpServletRequest,httpServletResponse,result);
    }
}
